package com.shiwaixiangcun.customer.module.ticket;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.SiteDetail;
import com.shiwaixiangcun.customer.glide.ImageDisplayUtil;
import com.shiwaixiangcun.customer.utils.DisplayUtil;
import com.shiwaixiangcun.customer.widget.ZoomImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author   Administrator
 * Date     2017/12/15
 * Desc     车站信息页面
 *
 * @author Administrator
 */

public class SiteInfoActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    @BindView(R.id.iv_close_big_image)
    ImageView mIvCloseBigImage;
    @BindView(R.id.ziv_image)
    ZoomImageView mZivImage;
    @BindView(R.id.id_viewpager)
    ViewPager mIdViewpager;
    @BindView(R.id.tv_site_title)
    TextView mTvSiteTitle;
    @BindView(R.id.tv_site_info)
    TextView mTvSiteInfo;
    @BindView(R.id.layout_indicator)
    LinearLayout mLayoutIndicator;
    @BindView(R.id.activity_big_iamge)
    LinearLayout mActivityBigIamge;


    private ArrayList<SiteDetail> mSiteInfos;
    private ArrayList<String> mImageList;
    private ArrayList<String> mSiteTitle;
    private ArrayList<String> mSiteName;
    private ImageView[] mImageViews;
    private ImageView[] tips;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_info);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        mSiteInfos = new ArrayList<>();
        mImageList = new ArrayList<>();
        mSiteName = new ArrayList<>();
        mSiteTitle = new ArrayList<>();

        Bundle extras = intent.getExtras();
        if (extras != null) {
            mSiteInfos = extras.getParcelableArrayList("siteInfo");
        }

        if (mSiteInfos == null || mSiteInfos.size() == 0) {
            return;
        }
        for (SiteDetail siteDetail : mSiteInfos) {
            mSiteTitle.add(siteDetail.getSiteTitle());
            mImageList.add(siteDetail.getPath());
            mSiteName.add(siteDetail.getSiteName());
        }
        mImageViews = new ImageView[mImageList.size()];
        initViewAndEvent();
        initData();
        mIdViewpager.setCurrentItem(0);
        mTvSiteInfo.setText(mSiteName.get(0));
        mTvSiteTitle.setText(mSiteTitle.get(0));
    }

    private void initViewAndEvent() {
        mIvCloseBigImage.setOnClickListener(this);


        mIdViewpager.setOnPageChangeListener(this);
        mIdViewpager.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ZoomImageView imageView = new ZoomImageView(
                        getApplicationContext());
                ImageDisplayUtil.showImageView(mContext, mImageList.get(position), imageView);
                container.addView(imageView);
                mImageViews[position] = imageView;
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView(mImageViews[position]);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return mImageList.size();
            }
        });

    }

    private void initData() {
        //将点点加入到ViewGroup中
        tips = new ImageView[mImageList.size()];
        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);


            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DisplayUtil.INSTANCE.dip2px(mContext, 8), DisplayUtil.INSTANCE.dip2px(mContext, 8));
            params.leftMargin = 8;
            params.rightMargin = 8;
            params.gravity = Gravity.CENTER;
            tips[i] = imageView;
            if (i == 0) {
                tips[i].setImageResource(R.drawable.selector_indicator_selected);
            } else {
                tips[i].setImageResource(R.drawable.selector_indicator_unselected);
            }


            mLayoutIndicator.addView(imageView, params);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.iv_close_big_image:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setImageBackground(position % mImageViews.length);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    /**
     * 设置选中的tip的背景
     *
     * @param selectItems
     */
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setImageResource(R.drawable.selector_indicator_selected);
            } else {
                tips[i].setImageResource(R.drawable.selector_indicator_unselected);
            }
        }
        mTvSiteInfo.setText(mSiteName.get(selectItems));
        mTvSiteTitle.setText(mSiteTitle.get(selectItems));
    }

}
